package com.example.imagesteganographylibrary.Text;

import com.example.imagesteganographylibrary.Text.AsyncTaskCallback.TextEncodingCallback;
import com.example.imagesteganographylibrary.Utils.MergePixelMap;
import com.example.imagesteganographylibrary.Utils.Utility;
import ohos.agp.components.ProgressBar;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.InnerEvent;
import ohos.media.image.PixelMap;

import java.util.List;

/**
 * In this class all those method in EncodeDecode class are used to encode secret message in image.
 * All the tasks will run in background.
 */
public class TextEncoding {
    private final ImageSteganography result;
    private final TextEncodingCallback callbackInterface;
    private final CommonDialog progressDialog;
    TaskDispatcher taskDispatcher;

    /**
     * TextEncoding
     *
     * @param abilitySlice abilitySlice
     * @param callbackInterface    callbackInterface
     * @param progressBar progressBar
     */
    public TextEncoding(Context abilitySlice, TextEncodingCallback callbackInterface, ProgressBar progressBar) {
        super();
        this.progressDialog = new CommonDialog(abilitySlice);
        this.callbackInterface = callbackInterface;
        this.result = new ImageSteganography();
        taskDispatcher = abilitySlice.getMainTaskDispatcher();
    }

    /**
     * TextEncoding
     *
     * @param abilitySlice abilitySlice
     * @param callbackInterface    callbackInterface
     */
    public TextEncoding(Context abilitySlice, TextEncodingCallback callbackInterface) {
        super();
        this.progressDialog = new CommonDialog(abilitySlice);
        this.callbackInterface = callbackInterface;
        this.result = new ImageSteganography();
        taskDispatcher = abilitySlice.getMainTaskDispatcher();
    }

    /**
     * TextEncoding
     *
     * @param imageSteganography imageSteganography
     */
    public void inBack(ImageSteganography imageSteganography) {
        callbackInterface.onStartTextEncoding();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ImageSteganography textStegnography = imageSteganography;
                //getting image bitmap
                PixelMap pixelMap = textStegnography.getImage();
                int originalWidth = pixelMap.getImageInfo().size.width;
                int originalHeight = pixelMap.getImageInfo().size.height;
                //splitting bitmap
                List<MergePixelMap.PixelMapSlice> src_list = Utility.splitImage(pixelMap);

                //encoding encrypted compressed message into image
                List<MergePixelMap.PixelMapSlice> encoded_list = EncodeDecode.encodeMessage(src_list, textStegnography.getEncrypted_message(), new EncodeDecode.ProgressHandler() {
                    @Override
                    public void setTotal(int tot) {
                        System.out.println("progress:" + tot);
                    }

                    @Override
                    public void increment(int inc) {

                    }

                    @Override
                    public void finished() {
                    }
                });

                // merging the split encoded image
                PixelMap srcEncoded = Utility.mergeImage(encoded_list, originalHeight, originalWidth);
                //free Memory
                for (MergePixelMap.PixelMapSlice bitm : src_list)
                    bitm.getPixelMap().release();
                result.setEncoded_image(srcEncoded);
                result.setEncoded(true);
                taskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        callbackInterface.onCompleteTextEncoding(result);
                    }
                });
            }
        }).start();
    }
}
